gameUtilID = 'gameUtilOther';


function FMLoad()
{
	gameUtilJS = document.getElementById(gameUtilID);
	gameContentJS = document.getElementById(gameContentID);
	
	/*gameUtilJS.style.visibility = "hidden";
	gameUtilJS.contentWindow.document.body.innerHTML = 'a';
	gameUtilJS.contentWindow.document.designMode = "on";
	gameUtilJS.contentWindow.addEventListener('keypress', FMKeyPressHandler, false);
	gameUtilJS.contentWindow.addEventListener('keydown', FMKeyDownHandler, false);
	gameUtilJS.contentWindow.addEventListener('keyup', FMKeyUpHandler, false);
	window.addEventListener("DOMMouseScroll", FMWheel, false); 
	window.addEventListener("mousedown", FMMouseDownHandler, true); */
}
function FMChangeGameSize(width, height)
{
	gameContentJS.setAttribute("width", width);
	gameContentJS.setAttribute("height", height);
	window.resizeBy(width - prevWidth, height - prevHeight );
	prevWidth = width;
	prevHeight = height;
}

function FMMouseDownHandler(oevent)
{
    if (oevent.button != 0) 
    {
		FMKillEvents(oevent);
		if(oevent.target.id == appName) 
		{
    		gameContentJS.openRightClick();
		}
	}
}

function FMKillEvents (eventObject) 
{
	if(eventObject) 
	{
		if (eventObject.stopPropagation) eventObject.stopPropagation();
		if (eventObject.preventDefault) eventObject.preventDefault();
		if (eventObject.preventCapture) eventObject.preventCapture();
	   	if (eventObject.preventBubble) eventObject.preventBubble();
	}
}

function FMKeyDownHandler(oEvent)
{
	var sChar = String.fromCharCode(oEvent.which).toLowerCase();

    if (oEvent.keyCode == 0)
    {
        if (!deadKeyWalking)
            gameUtilJS.contentWindow.document.body.innerHTML = '';
        deadKeyWalking = true;
        return;
    }
	if (oEvent.metaKey == true)
	{
		if (isMZ3)	// Firefox 3 sends key-down's but not key-up's for these
		{
			if (sChar == 'v')
			{
				setTimeout('FMOnPaste()', 0);
				return;
			}
			else if (sChar == 'c')
			{
				gameContentJS.onBrowserCopy(false);
				return;
			}
			else if (sChar == 'x')
			{
				gameContentJS.onBrowserCut(false);
				return;
			}
		}
		
		var sel = gameContentJS.getSelection();
		gameUtilJS.contentWindow.document.body.innerHTML = sel;
		if (sel != '')
		{
			FMSelect();
		}
		else
		{
			FMUnselect();
		}
	}
	// Send key-down for ctrl/shift/option/cmd keys
	var kc = oEvent.keyCode;
	if ( kc == 16 || kc == 17 || kc == 18 || kc == 224)
	{
		try {
			gameContentJS.onKeyDownMZ(oEvent.keyCode, oEvent.ctrlKey, oEvent.altKey, oEvent.shiftKey);
		} catch (e) {
			alert(e.name);
		}
	} 
}

function FMKeyUpHandler(oEvent)
{
	var sChar = String.fromCharCode(oEvent.which).toLowerCase();
	if (oEvent.metaKey == true)
	{
		if (isMZ2)	// Firefox 2 sends key-up's but not key-down's for these
		{
			if (sChar == 'v')
			{
				setTimeout('FMOnPaste()', 0);
			}
			else if (sChar == 'c')
			{
				gameContentJS.onBrowserCopy(false);
			}
			else if (sChar == 'x')
			{
				gameContentJS.onBrowserCut(false);
			}
		}
	}
	else
	{
        var charCode = oEvent.charCode;

        if (deadKeyWalking)
        {
            var sel = gameUtilJS.contentWindow.getSelection()
            {
                var tc = sel.focusNode.textContent;
                var myCharCode = tc.charCodeAt(0);
                if (myCharCode >= 192 && myCharCode <= 255)
                {
                    charCode = myCharCode;
                    gameContentJS.onKeyPressMZ(charCode, oEvent.keyCode, oEvent.ctrlKey, oEvent.metaKey, oEvent.shiftKey);
                    deadKeyWalking = false;
                    return;
                }
            }
        }

		try {
			gameContentJS.onKeyUpMZ(charCode, oEvent.keyCode, oEvent.ctrlKey, oEvent.altKey, oEvent.shiftKey);
		} catch (e) {
			alert(e.name);
		}
	}
}

// a variable that keeps track of whether we are expecting a dead key
// to arrive.
var deadKeyWalking = false;

function FMKeyPressHandler(oEvent)
{
    deadKeyWalking = false; // if we got a key press, then any pending dead key will be handled here
	var sChar = String.fromCharCode(oEvent.which).toLowerCase();
	if (oEvent.metaKey == false)
	{
		try {
			// Adjust key code as necessary so that ctrl-enter comes through correctly
			var kCode = oEvent.keyCode;
			if (kCode == 77 && oEvent.ctrlKey == true)
				kCode = 13;
			else if (kCode == 0)
				kCode = oEvent.charCode;
			gameContentJS.onKeyPressMZ(oEvent.charCode, kCode, oEvent.ctrlKey, oEvent.metaKey, oEvent.shiftKey);
		} catch (e) {
			alert(e.name);
		}
		if (gameUtilJS.contentWindow.document.body.innerHTML == '<br>' || gameUtilJS.contentWindow.document.body.innerHTML.length > 10)
			gameUtilJS.contentWindow.document.body.innerHTML = 'a';
	}
	
	// If there is more than one tab open, then Ctrl-Tab will switch to the next tab by default
	// So cancel the event before that happens. This has to be done in the KeyPress handler -- KeyUp is too late
	if (oEvent.keyCode == 9 && oEvent.ctrlKey == true)
		FMStopEvent(oEvent);
}

function FMStopEvent(oEvent)
{
	oEvent.preventDefault();
	oEvent.stopPropagation();
}

function FMReturnFocusToEditor()
{
//	alert('FMReturnFocusToEditor');
	var result = true;
	try
	{
		gameUtilJS.contentWindow.focus();
		setTimeout('gameUtilJS.contentWindow.focus();', 0);
		setTimeout('gameUtilJS.contentWindow.focus();', 0);
	}
	catch (e)
	{
		alert(e.name);
		if (e.name == "TypeError")
		{
			if (typeof(gameContentJS) == "object")
				setTimeout('gameContentJS.focus();', 0);
			result = false;
		}
	}
	return result;
}

function FMReturnFocusToCMM()
{
}

function FMWheel(event)
{
     if (!event) event = window.event; // For IE
     var delta = 0;
     if (event.wheelDelta)             // IE case
          delta = event.wheelDelta;
     else if (event.detail)            // Mozilla case
          delta = -event.detail;       // Mozilla and IE deltas differ in sign
    if (delta)
    	delta = delta*3;
//    doTrace('delta='+delta);
     if (delta) {
          gameContentJS.onMouseWheel(delta);
     }
}